package com.zhongshengnetwork.rightbe.common.customview;



import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;

import com.zhongshengnetwork.rightbe.R;

import java.util.List;

public class PublishSelectPicPopupWindow extends PopupWindow {


	private Button firstButton;
	private Button secondButton;
	private Button thirdButton;
	private Button fourthButton;
	private View mMenuView;

	@SuppressWarnings("deprecation")
	public PublishSelectPicPopupWindow(Activity context, OnClickListener itemsOnClick, List<String> names) {
		super(context);
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mMenuView = inflater.inflate(R.layout.publish_dialog, null);
		firstButton = (Button) mMenuView.findViewById(R.id.first_button);
		secondButton = (Button) mMenuView.findViewById(R.id.second_button);
		thirdButton=(Button) mMenuView.findViewById(R.id.third_button);
		fourthButton = (Button) mMenuView.findViewById(R.id.fourth_button);
		if (names.size() == 4){
			firstButton.setText(names.get(0));
			secondButton.setText(names.get(1));
			thirdButton.setText(names.get(2));
			fourthButton.setText(names.get(3));
			fourthButton.setOnClickListener(new OnClickListener() {

				public void onClick(View v) {
					dismiss();
				}
			});
			//设置按钮监听
			firstButton.setOnClickListener(itemsOnClick);
			secondButton.setOnClickListener(itemsOnClick);
			thirdButton.setOnClickListener(itemsOnClick);
			fourthButton.setOnClickListener(itemsOnClick);
			/*
			firstButton.setTextColor(context.getResources().getColor(R.color.mainColor));
			secondButton.setTextColor(context.getResources().getColor(R.color.mainColor));
			thirdButton.setTextColor(context.getResources().getColor(R.color.mainColor));
			*/
			fourthButton.setTextColor(context.getResources().getColor(R.color.defaultLightColor));
		}else if (names.size() == 3){
			firstButton.setText(names.get(0));
			secondButton.setText(names.get(1));
			thirdButton.setText(names.get(2));
			thirdButton.setTextColor(context.getResources().getColor(R.color.defaultLightColor));
			fourthButton.setVisibility(View.GONE);
			firstButton.setOnClickListener(itemsOnClick);
			secondButton.setOnClickListener(itemsOnClick);
			thirdButton.setOnClickListener(itemsOnClick);
			/*
			firstButton.setTextColor(context.getResources().getColor(R.color.mainColor));
			secondButton.setTextColor(context.getResources().getColor(R.color.mainColor));
			*/
			thirdButton.setTextColor(context.getResources().getColor(R.color.defaultLightColor));
		}else if (names.size() == 2){
			firstButton.setText(names.get(0));
			secondButton.setText(names.get(1));
			thirdButton.setVisibility(View.GONE);
			fourthButton.setVisibility(View.GONE);
			firstButton.setOnClickListener(itemsOnClick);
			secondButton.setOnClickListener(itemsOnClick);
			//firstButton.setTextColor(context.getResources().getColor(R.color.mainColor));
			secondButton.setTextColor(context.getResources().getColor(R.color.defaultLightColor));
		}
		//设置SelectPicPopupWindow的View
		this.setContentView(mMenuView);
		//设置SelectPicPopupWindow弹出窗体的宽
		this.setWidth(LayoutParams.FILL_PARENT);
		//设置SelectPicPopupWindow弹出窗体的高
		this.setHeight(LayoutParams.FILL_PARENT);
		//设置SelectPicPopupWindow弹出窗体可点击
		this.setFocusable(true);
		//设置SelectPicPopupWindow弹出窗体动画效果
		this.setAnimationStyle(R.style.AnimBottom);
		//实例化一个ColorDrawable颜色为半透明
		ColorDrawable dw = new ColorDrawable(0xe0f0f0f0);
		//设置SelectPicPopupWindow弹出窗体的背景
		//this.setBackgroundDrawable(dw);
		//mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
		mMenuView.setOnTouchListener(new OnTouchListener() {
			public boolean onTouch(View v, MotionEvent event) {
				
				int height = mMenuView.findViewById(R.id.pop_layout).getTop();
				int y=(int) event.getY();
				if(event.getAction()== MotionEvent.ACTION_UP){
					/*
					if(y<height){
						dismiss();
					}
					*/
					dismiss();
				}				
				return true;
			}
		});

	}

}
